04. Using a Class in C++ [Demo]
Using a Class in a Program
Let's start with the big picture. We've written code below with a fully implemented class and a main.cpp file that uses the class. Even though you might not be familiar with some of the C++ syntax, your knowledge of Python object oriented programming should help you understand what the C++ code is doing.
Study the code and then run the code as instructed below.
Main.cpp and Gaussian.cpp
At the bottom of the page, you'll find a main.cpp file and a gaussian.cpp file so that you can see how they work together.
The gaussian.cpp file contains the class definition including all the variables and functions that the Gaussian class needs. You would make a similar file in Python probably called gaussian.py.
The main.cpp file uses the class to run some calculations. You'll see one important difference between C++ and Python. In C++, you need to declare your class before you can use the class. Both main.cpp and gaussian.cpp have the same class declaration at the top of their files:
class Gaussian
{
private:
float mu, sigma2;
public:
// constructor functions
Gaussian ();
Gaussian (float, float);
// change value of average and standard deviation
void setMu(float);
void setSigma2(float);
// output value of average and standard deviation
float getMu();
float getSigma2();
// functions to evaluate
float evaluate (float);
Gaussian mul (Gaussian);
Gaussian add (Gaussian);
};
Below are all of the files that would be used in this code so that you can see the relationship between the main.cpp and gaussian.cpp
You can't see it on the backend, but this program is first being compiled via the command:
g++ main.cpp gaussian.cpp
Study the two files, and then click the "Test Run" button to run the code and see the results.
Start Quiz:
#include <iostream>
// class declaration
class Gaussian
{
private:
float mu, sigma2;
public:
// constructor functions
Gaussian ();
Gaussian (float, float);
// change value of average and standard deviation
void setMu(float);
void setSigma2(float);
// output value of average and standard deviation
float getMu();
float getSigma2();
// functions to evaluate
float evaluate (float);
Gaussian mul (Gaussian);
Gaussian add (Gaussian);
};
int main ()
{
Gaussian mygaussian(30.0,100.0);
Gaussian othergaussian(10.0,25.0);
std::cout << "average " << mygaussian.getMu() << std::endl;
std::cout << "evaluation " << mygaussian.evaluate(15.0) << std::endl;
std::cout << "mul results variance " << mygaussian.mul(othergaussian).getSigma2() << std::endl;
std::cout << "mul results average " << mygaussian.mul(othergaussian).getMu() << std::endl;
std::cout << "add results variance " << mygaussian.add(othergaussian).getSigma2() << std::endl;
std::cout << "add results average " << mygaussian.add(othergaussian).getMu() << std::endl;
return 0;
}
#include <math.h> /* sqrt, exp */
// class declaration
class Gaussian
{
private:
float mu, sigma2;
public:
// constructor functions
Gaussian ();
Gaussian (float, float);
// change value of average and standard deviation
void setMu(float);
void setSigma2(float);
// output value of average and standard deviation
float getMu();
float getSigma2();
// functions to evaluate
float evaluate (float);
Gaussian mul (Gaussian);
Gaussian add (Gaussian);
};
Gaussian::Gaussian() {
mu = 0;
sigma2 = 1;
}
Gaussian::Gaussian (float average, float sigma) {
mu = average;
sigma2 = sigma;
}
void Gaussian::setMu (float average) {
mu = average;
}
void Gaussian::setSigma2 (float sigma) {
sigma2 = sigma;
}
float Gaussian::getMu () {
return mu;
}
float Gaussian::getSigma2() {
return sigma2;
}
float Gaussian::evaluate(float x) {
float coefficient;
float exponential;
coefficient = 1.0 / sqrt (2.0 * M_PI * sigma2);
exponential = exp ( pow (-0.5 * (x - mu), 2) / sigma2 );
return coefficient * exponential;
}
Gaussian Gaussian::mul(Gaussian other) {
float denominator;
float numerator;
float new_mu;
float new_var;
denominator = sigma2 + other.getSigma2();
numerator = mu * other.getSigma2() + other.getMu() * sigma2;
new_mu = numerator / denominator;
new_var = 1.0 / ( (1.0 / sigma2) + (1.0 / other.sigma2) );
return Gaussian(new_mu, new_var);
}
Gaussian Gaussian::add(Gaussian other) {
float new_mu;
float new_sigma2;
new_mu = mu + other.getMu();
new_sigma2 = sigma2 + other.getSigma2();
return Gaussian(new_mu, new_sigma2);
}